//const baseURL = 'https://frp-act.top:24026'
const baseURL = 'http://localhost:8080'
const request = (options) => {
  return new Promise((resolve, reject) => {
    let url = baseURL + options.url
    if (options.method === 'GET' && options.data) {
      const queryString = Object.entries(options.data)
        .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
        .join('&')
      url += `?${queryString}`
    }
    
    uni.request({
      url,
      method: options.method || 'GET',
      data: options.method === 'POST' ? JSON.stringify(options.data) : undefined,
      header: {
        'Content-Type': 'application/json;charset=UTF-8',
        ...options.header
      },
      withCredentials: true,
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data)
        } else {
          uni.showToast({
            title: res.data.message || '请求失败',
            icon: 'none'
          })
          reject(res.data)
        }
      },
      fail: (err) => {
        console.error('Request Error:', err)
        uni.showToast({
          title: '网络错误',
          icon: 'none'
        })
        reject(err)
      }
    })
  })
}

export default request